package com.qwc.blog.controller.back;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qwc.blog.common.constant.CommonConstant;
import com.qwc.blog.common.validate.IdValid;
import com.qwc.blog.entity.Content;
import com.qwc.blog.entity.Posts;
import com.qwc.blog.entity.User;
import com.qwc.blog.entity.vo.posts.AddPostDto;
import com.qwc.blog.entity.vo.posts.PostsListReqDto;
import com.qwc.blog.security.service.LoginService;
import com.qwc.blog.service.IContentService;
import com.qwc.blog.service.IPostsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.qwc.blog.common.constant.ResponseConstant.*;

/**
 * <p>
 * 文章元信息表 前端控制器
 * </p>
 *
 * @author qwc
 * @since 2021-11-01
 */
@RestController
@Slf4j
@RequestMapping("/blog/posts")
@Validated
public class PostsController {

    @Autowired
    private IPostsService postsService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private IContentService contentService;

    @GetMapping("/list")
    public ResponseEntity<Object> list(PostsListReqDto dto) {
        Map<String, Object> map = new HashMap<>(4);
        Page<Posts> postsPage = postsService.getPostsList(dto);
        map.put(ROWS, postsPage.getRecords());
        map.put(TOTAL, postsPage.getTotal());
        return ResponseEntity.ok(map);
    }

    @PostMapping
    public ResponseEntity<Object> addPosts(@RequestBody @Validated AddPostDto dto) {
        User currentUser = loginService.getCurrentUser();
        postsService.savePosts(dto, currentUser);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/update/comment")
    public ResponseEntity<Object> updateComment(Integer postsId, String isComment) {
        postsService.update(Wrappers.<Posts>lambdaUpdate()
                .set(Posts::getIsComment, isComment)
                .eq(Posts::getPostsId, postsId));
        return ResponseEntity.ok().build();
    }

    @PutMapping("/release/{postsId}")
    public ResponseEntity<Object> releasePosts(@PathVariable Integer[] postsId) {
        postsService.update(Wrappers.<Posts>lambdaUpdate()
                .set(Posts::getStatus, CommonConstant.YES_INT)
                .in(Posts::getPostsId, Arrays.asList(postsId)));
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("{postsId}")
    public ResponseEntity<Object> deletePosts(@PathVariable @NotEmpty(message = "请选择删除文章") Integer[] postsId) {
        postsService.removePostsAndPostsCategory(Arrays.asList(postsId));
        return ResponseEntity.ok().build();
    }

    @PutMapping("/off/{postsId}")
    public ResponseEntity<Object> offPosts(@PathVariable Integer postsId) {
        postsService.update(Wrappers.<Posts>lambdaUpdate()
                .set(Posts::getStatus, CommonConstant.NO_INT)
                .in(Posts::getPostsId, Arrays.asList(postsId)));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{postsId}")
    public ResponseEntity<Object> getPosts(@PathVariable Integer postsId) {
        Map<String, Object> map = new HashMap<>(8);
        Content content = contentService.getOne(Wrappers.<Content>lambdaQuery().eq(Content::getPostsId, postsId));
        map.put(CONTENT, Objects.nonNull(content) ? content.getContent() : StringUtils.EMPTY);
        map.put(TAGS, postsService.getTags(postsId));
        map.put(CATEGORIES, postsService.getCategories(postsId));
        map.put(DATA, postsService.getById(postsId));
        return ResponseEntity.ok(map);
    }

    @PutMapping
    public ResponseEntity<Object> updatePosts(@RequestBody @Validated(IdValid.class) AddPostDto dto) {
        User currentUser = loginService.getCurrentUser();
        postsService.updatePosts(dto, currentUser);
        return ResponseEntity.ok().build();
    }
}
